package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.LactationBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ILactationService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 哺乳假设置
 * @author 张鑫
 * 2017-10-30
 *
 */
@Controller
@RequestMapping("/lactation")
public class LactationController {
	
	@Resource
	private ILactationService lactationService;
	
	/**
	 * 新增哺乳假
	 * @param request
	 * @param lactationBean
	 * @return
	 */
	@RequestMapping(value = "/saveLactation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveLactation(HttpServletRequest request, @RequestBody LactationBean lactationBean){
		if (lactationBean.getEmployeeId()==null
				||lactationBean.getRequestType()==null
				||lactationBean.getHbornStartDate()==null
				||lactationBean.getEndDate()==null
				||lactationBean.getHours()==null) {
			throw new ResultFaultException("0_00001");
		}
		if (lactationService.getVerifySave(lactationBean)) {
			throw new ResultFaultException("6_00004");
		}
		if (lactationService.saveLactation(lactationBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00007");
	}

	/**
	 * 删除哺乳假
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteLactation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteLactation(HttpServletRequest request){
		String lactationId=request.getParameter("lactationId");
		if (StringUtils.isBlank(lactationId)) {
			throw new ResultFaultException("0_00001");
		}
		if (lactationService.deleteLactation(lactationId)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00009");
	}
	
	/**
	 * 修改哺乳假
	 * @param request
	 * @param lactationBean
	 * @return
	 */
	@RequestMapping(value = "/updateLactation.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateLactation(HttpServletRequest request, @RequestBody LactationBean lactationBean){
		if (lactationBean.getLactationId()==null
				||lactationBean.getHbornStartDate()==null
				||lactationBean.getEndDate()==null
				||lactationBean.getHours()==null
				||lactationBean.getUpdateTime()==null) {
			throw new ResultFaultException("0_00001");
		}
		if (lactationService.updateLactation(lactationBean)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00008");
	}

	/**
	 * 查询哺乳假信息
	 * @param request
	 * @param lactationBean
	 * @return
	 */
	@RequestMapping(value = "/getLactationList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getLactationList(HttpServletRequest request, @RequestBody LactationBean lactationBean){
		if (lactationBean.getPageCount()==null
				||lactationBean.getPageNo()==null) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo=lactationBean.getPageNo();
		int pageCount=lactationBean.getPageCount();
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		lactationBean.setStartNum(startNum);
		return ResultMap.success(lactationService.getLactationList(lactationBean));
	}

}
